<script lang="ts" setup>
import { ref } from 'vue'
import { ElAvatar, ElButton, ElInput } from 'element-plus'

const userAvatar = ref('path_to_default_avatar.jpg')
const questionInput = ref('')
const replyInput = ref('')
const questions = ref([
  {
    userAvatar: 'path_to_user_avatar.jpg',
    username: '张三',
    content: '这是一个示例问题的内容',
    time: '2024-5-01 10:00:00',
    answers: [
      {
        username: '李四',
        userAvatar: 'path_to_user_avatar.jpg',
        content: '回复 @张三: 这是一个示例回复的内容1',
        time: '2024-5-01 11:00:00'
      },
      {
        username: '王五',
        userAvatar: 'path_to_user_avatar.jpg',
        content: '回复 @李四: 这是一个示例回复的内容2',
        time: '2024-5-01 11:00:00'
      }
    ]
  },
  {
    userAvatar: 'path_to_user_avatar.jpg',
    username: '小帅',
    content: '这是另一个示例问题的内容1',
    time: '2024-5-01 12:00:00',
    answers: []
  }
])

//当前问题板块索引
const activeIndex = ref(-1)
//当前回复用户名
const currentReplyUsername = ref('')

//发布问题
const askQuestion = () => {
  if (questionInput.value.trim() === '') return
  questions.value.unshift({
    userAvatar: userAvatar.value,
    username: '张三',
    content: questionInput.value.trim(),
    time: getCurrentDate(),
    answers: []
  })
  questionInput.value = ''
}

//显示回复输入框
const showReplyInput = (index: number, replyTarget: string) => {
  activeIndex.value = index
  currentReplyUsername.value = replyTarget
}

//回复问题
const replyQuestion = () => {
  if (replyInput.value.trim() === '') return
  questions.value[activeIndex.value].answers.unshift({
    username: '我自己',
    userAvatar: userAvatar.value,
    content: '回复 @' + currentReplyUsername.value + ': ' + replyInput.value.trim(),
    time: getCurrentDate()
  })
  replyInput.value = ''
}

function getCurrentDate() {
  const currentDate = new Date()
  const year = currentDate.getFullYear() // 获取当前年份
  const month = currentDate.getMonth() + 1 // 获取当前月份，注意月份从0开始计数，需要加1
  const day = currentDate.getDate() // 获取当前日期
  const hours = currentDate.getHours() // 获取当前小时
  const minutes = currentDate.getMinutes() // 获取当前分钟
  const seconds = currentDate.getSeconds() // 获取当前秒
  // console.log(`${year}-${month}-${day} ${hours}:${minutes}:${seconds}`);
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
}
</script>

<template>
  <div class="qa-app">
    <!-- 顶部提问区 -->
    <div class="input-section">
      <el-avatar :src="userAvatar" size="default" style="margin-top: 0.5em"></el-avatar>
      <div style="display: flex; flex-wrap: wrap; flex: 1; gap: 0.5em">
        <el-input
          v-model="questionInput"
          :autosize="{ minRows: 2, maxRows: 4 }"
          placeholder="请输入您的问题"
          style="width: 100%"
          type="textarea"
        />
        <el-button style="float: right" type="primary" @click="askQuestion">提问</el-button>
      </div>
    </div>

    <!-- 问答列表 -->
    <div class="qa-list">
      <div v-for="(question, index) in questions" :key="index" class="question-block">
        <!-- 分割线 -->
        <el-divider />
        <!-- 问题内容 -->
        <div class="content-box">
          <el-avatar :src="question.userAvatar" size="default"></el-avatar>
          <!-- 文本内容 -->
          <div class="content-info">
            <span class="question-title">{{ question.username }}</span>
            <div class="question-content">{{ question.content }}</div>
            <!-- 底部信息 -->
            <div class="content-footer">
              <span class="question-time">{{ question.time }}</span>
              <el-button link type="primary" @click="showReplyInput(index, question.username)">
                回复
              </el-button>
            </div>
          </div>
        </div>
        <!-- 回复列表 -->
        <div class="answer-list">
          <div v-for="(answer, idx) in question.answers" :key="idx" class="answer">
            <div class="content-box">
              <el-avatar :src="answer.userAvatar" size="small"></el-avatar>
              <!-- 文本内容 -->
              <div class="content-info">
                <span class="question-title">{{ answer.username }}</span>
                <div class="answer-content">{{ answer.content }}</div>
                <!-- 底部信息 -->
                <div class="content-footer">
                  <span class="answer-time">{{ answer.time }}</span>
                  <el-button link type="primary" @click="showReplyInput(index, answer.username)">
                    回复
                  </el-button>
                </div>
              </div>
            </div>
          </div>
        </div>
        <!-- 回复输入框 -->
        <div v-if="activeIndex == index" class="input-section">
          <el-avatar :src="userAvatar" size="default" style="margin-top: 0.5em"></el-avatar>
          <div style="display: flex; flex-wrap: wrap; flex: 1; gap: 0.5em">
            <span class="question-title" style="color: dodgerblue"
              >回复 @{{ currentReplyUsername }}</span
            >
            <el-input
              v-model="replyInput"
              :autosize="{ minRows: 2, maxRows: 4 }"
              placeholder="请输入回复内容"
              style="width: 100%"
              type="textarea"
            />
            <el-button style="float: right" type="primary" @click="replyQuestion">回复</el-button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style>
.qa-app {
  max-width: 900px;
  margin: 0 auto;
}

.input-section {
  display: flex;
  margin: 2em 0;
  gap: 0.8em;
}

.content-box {
  display: flex;
  flex-wrap: wrap;
  gap: 0.8em;
}

.content-info {
  display: flex;
  flex-direction: column;
  flex: 2;
  gap: 0.5em;
}

.question-block {
  margin-bottom: 20px;
}

.question-title {
  font-size: 0.8em;
}

.question-time,
.answer-time {
  color: #888;
  font-size: 0.8em;
}

.answer {
  margin-bottom: 0.8em;
}

.answer-list {
  margin-top: 1em;
  margin-left: 3em;
}

.content-footer {
  display: flex;
  align-items: center;
  gap: 1em;
}
</style>
